package com.cw.jeeyt.controller.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.bo.api.ApiDatabaseBo;
import com.cw.jeeyt.service.intf.api.ApiDatabaseService;
import com.cw.lang.common.log.Log;
import com.cw.lang.common.log.TenantLog;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * api数据库控制层
 *
 * @author: chenw
 * @create: 2018-11-25 23:04
 */
@Controller
@RequestMapping("/api/apiDatabase")
public class ApiDatabaseController extends BaseController {
    private static Log log = TenantLog.getLogger(ApiDatabaseController.class);
    @Autowired
    private ApiDatabaseService apiDatabaseService;

    /**
     * 分页查询数据库信息
     *
     * @param pageParam
     * @return
     */
    @PostMapping("/pageList")
    public JsonResultDto queryPage(@RequestBody ApiDatabaseBo pageParam) {
        JsonResultDto apiResult = new JsonResultDto(false);
        PageInfo<ApiDatabaseBo> pageInfo = apiDatabaseService.queryPageInfo(pageParam, pageParam.getPageNum(),
            pageParam.getPageSize());
        apiResult.setTotal(pageInfo.getTotal());
        apiResult.setPageSize(pageParam.getPageSize());
        apiResult.setSuccess(true);
        apiResult.setTableData(pageInfo.getList());
        return apiResult;
    }

    /**
     * 新增数据库信息
     *
     * @param apiDatabase
     * @return
     */
    @PostMapping("/database")
    public JsonResultDto saveDatabase(@RequestBody ApiDatabaseBo apiDatabase) {
        JsonResultDto apiResult = new JsonResultDto(false);
        boolean success = apiDatabaseService.insert(apiDatabase);
        apiResult.setSuccess(success);
        return apiResult;
    }

    /**
     * 修改数据库信息
     *
     * @param apiDatabase
     * @return
     */
    @PutMapping("/database")
    public JsonResultDto editDatabase(@RequestBody ApiDatabaseBo apiDatabase) {
        JsonResultDto apiResult = new JsonResultDto(false);
        boolean success = apiDatabaseService.update(apiDatabase);
        apiResult.setSuccess(success);
        return apiResult;
    }

    /**
     * 删除数据库信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResultDto deleteDatabase(@PathVariable String id) {
        JsonResultDto apiResult = new JsonResultDto(false);
        if (StringUtils.isEmpty(id)) {
            apiResult.setMsg("入参有误，请检查！");
            return apiResult;
        }
        ApiDatabaseBo apiDatabaseBo = new ApiDatabaseBo();
        apiDatabaseBo.setId(Long.parseLong(id));
        boolean success = apiDatabaseService.deleteById(apiDatabaseBo);
        apiResult.setSuccess(success);
        return apiResult;
    }

    /**
     * 查询数据库信息详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public JsonResultDto queryDatabase(@PathVariable Long id) {
        JsonResultDto apiResult = new JsonResultDto(false);
        ApiDatabaseBo param = new ApiDatabaseBo();
        param.setId(id);
        ApiDatabaseBo result = apiDatabaseService.getById(param);
        apiResult.setSuccess(true);
        apiResult.setData(result);
        return apiResult;
    }
}
